// goodsInfo.js
const url = getApp().globalData.API_URL;
const Uid = getApp().globalData.uid;
const app = getApp();
Component({
  data: {
    dataInfo:{},
    kind:'',
    currentCategorySelect:'',
    categoryList: [
    {
      "name": "餐饮",
      "iconStyleName": "icon-canyin",
      "color": "#02c39a"
    },
    {
      "name": "交通",
      "iconStyleName": "icon-jiaotong",
      "color": "#02c39a"
    },
    {
      "name": "住房",
      "iconStyleName": "icon-zhufang",
      "color": "#02c39a"
    },
    {
      "name": "家庭",
      "iconStyleName": "icon-080jiating",
      "color": "#02c39a"
    },
    {
      "name": "宠物",
      "iconStyleName": "icon-chongwu",
      "color": "#02c39a"
    },
    {
      "name": "购物",
      "iconStyleName": "icon-gouwu",
      "color": "#02c39a"
    },
    {
      "name": "充值",
      "iconStyleName": "icon-chongzhi",
      "color": "#02c39a"
    },
    {
      "name": "水电",
      "iconStyleName": "icon-shuidian",
      "color": "#02c39a"
    },
    {
      "name": "医疗",
      "iconStyleName": "icon-yiliao",
      "color": "#02c39a"
    },
    {
      "name": "烟酒",
      "iconStyleName": "icon-yanjiu",
      "color": "#02c39a"
    },
    {
      "name": "旅游",
      "iconStyleName": "icon-lvyou",
      "color": "#02c39a"
    },
    {
      "name": "借出",
      "iconStyleName": "icon-jiechu",
      "color": "#02c39a"
    },
    {
      "name": "其他",
      "iconStyleName": "icon-qita",
      "color": "#02c39a"
    },
  ],
    categoryList2: [{
    "name": "工资",
    "iconStyleName": "icon-gongzi",
    "color": "#02c39a"
  },
  {
    "name": "兼职",
    "iconStyleName": "icon-jianzhi",
    "color": "#02c39a"
  },
  {
    "name": "理财",
    "iconStyleName": "icon-licai",
    "color": "#02c39a"
  },
  {
    "name": "礼金",
    "iconStyleName": "icon-lijin",
    "color": "#02c39a"
  },
  {
    "name": "其它",
    "iconStyleName": "icon-qita",
    "color": "#02c39a"
  },
  ],
    year: new Date().getFullYear(), 
    month: new Date().getMonth()+1, 
    day: new Date().getDate(), 
    // upKind: null,
  },
  methods: {
    //接收dataInfo，并渲染数据
    onLoad(options) {
      let dataInfo = JSON.parse(decodeURIComponent(options.dataInfo));
      console.log(dataInfo);
      dataInfo.caloricSum = dataInfo.caloricSum < 0 ? parseFloat(-dataInfo.caloricSum).toFixed(2) : dataInfo.caloricSum
      this.setData({
        dataInfo:dataInfo,
        kind: dataInfo.kind,
        currentCategorySelect: dataInfo.specification.trim(),
      });
      console.log(this.data.currentCategorySelect);
    },

    //切换支出\收入
    tabclick: function(e) {
      let kind = e.currentTarget.dataset.kind; // 获取点击事件携带的数据
      let dataInfo = this.data.dataInfo;
      if (kind === '0') {
        dataInfo.name = '餐饮';
        dataInfo.specification = 'icon-canyin';
          this.setData({
              kind: kind,
          });
      } else if (kind === '1') {
        dataInfo.name = '工资';
        dataInfo.specification = 'icon-gongzi';
          this.setData({
              kind: kind,
          });
      }
      dataInfo.kind = parseInt(kind);
      this.setData({
        dataInfo: dataInfo,
        currentCategorySelect:dataInfo.specification
    });
    },

    // 获取input
    handleInfoInput(e) {
      console.log('InfoInput值： ' + e.detail.value)
      let dataInfo = this.data.dataInfo;
      dataInfo.info = e.detail.value;
      this.setData({
      dataInfo: dataInfo,
      })
    },

    handleCaloricInput(e) {
      console.log('CaloricInput值： ' + e.detail.value)
      let dataInfo = this.data.dataInfo;
      dataInfo.caloricSum = e.detail.value;
      this.setData({
      dataInfo: dataInfo,
      })
    },

    //处理金额
    handleCaloric(dataInfo){
      if(this.data.kind == 0){
        dataInfo.caloricSum =  parseInt(dataInfo.caloricSum*(-100))
      }
      if(this.data.kind == 1){
          dataInfo.caloricSum =  parseInt(dataInfo.caloricSum*(100))
      }
      return dataInfo;
    },
    //选择种类
      categoryClick(e) {
        let categoryItem = e.currentTarget.dataset.item;
        let dataInfo = this.data.dataInfo;
        dataInfo.name = categoryItem.name;
        dataInfo.specification = categoryItem.iconStyleName;
        this.setData({
            currentCategorySelect: categoryItem.iconStyleName,
            dataInfo:dataInfo
        });
    },
    // 时间选择器
      showPicker(e) {
        const {
          mode
        } = e.currentTarget.dataset;
        this.setData({
          mode,
          [`${mode}Visible`]: true,
        });
      },
      hidePicker() {
        const {
          mode
        } = this.data;
        this.setData({
          [`${mode}Visible`]: false,
        });
      },
      onConfirm(e) {
        let dataInfo = this.data.dataInfo;
        let date = new Date(e.detail.value);
        let year = date.getFullYear();
        let month = date.getMonth() < 9 ? '0' + (date.getMonth()+1) :(date.getMonth()+1);
        let day = date.getDate() < 10 ? '0' + (date.getDate()) :(date.getDate());
        date = year + '-' + month+ '-' + day;
        dataInfo.date = date;
        this.setData({
          dataInfo:dataInfo
        });
        this.hidePicker();
      },
      onColumnChange(e) {
        console.log('pick', e.detail.value);
      },
      editGoods(){
        let dataInfo = this.data.dataInfo;
        if (dataInfo.caloricSum !== '') {
          var regex = /^\d+(\.\d{1,2})?$/;
          if(!regex.test(dataInfo.caloricSum)){
            wx.showToast({
              title: '金额输入格式有误',
              icon: 'none',
              duration: 500
          });
          return;
          }
        }
        if(dataInfo.caloricSum == ''){
          wx.showToast({
            title: '金额不能为空',
            icon: 'none',
            duration: 500
        });
        return; // 提前退出函数
        }
        let _this = this;
        wx.showModal({
          title: '确认编辑',
          content: '是否确认编辑？',
          showCancel: true,  // 是否显示取消按钮
          cancelText: '取消', // 取消按钮的文字，默认为"取消"
          confirmText: '确认', // 确认按钮的文字，默认为"确定"
          success(res) {
            if (res.confirm) {

              //处理时间
              dataInfo.date = parseInt(dataInfo.date.replace(/-/g, '')); 
              //处理金额
              dataInfo = _this.handleCaloric(dataInfo);
              console.log(dataInfo);

              wx.request({
                url: `${url}/sys-detail/upd`,
                data:dataInfo,
                method: 'POST',
                success: function (res) {
                  console.log('数据发送成功', res);
                  console.log('数据发送成功', dataInfo);
                  console.log(res.statusCode);
                  if (res.statusCode == 200) {
                    wx.showToast({
                      title: '编辑成功',
                      icon: 'success',
                      duration: 500 //持续的时间
                    })
                    setTimeout(function () {
                      wx.switchTab({
                        url: '../goodsListNew/goodsList'
                      });
                    }, 500)
                  } 
                  else {
                    wx.showToast({
                      title: '编辑失败',
                      icon: 'error',
                      duration: 500 //持续的时间
                    })
                    // 关闭当前页面，跳转页面。
                    setTimeout(function () {
                      wx.switchTab({
                        url: '../goodsListNew/goodsList'
                      });
                    }, 500)
                  }
                },
                fail: function (err) {
                  console.error('数据发送失败', err);
                  wx.showToast({
                    title: '失败!',
                    icon: 'error',
                    duration: 2000 //持续的时间
                  })
                },
              });
            } else if (res.cancel) {
              return;
            }
          }
        });
      }
  },
});